<?php
/**
 *
 */
class Sx_article extends CI_Controller {
	/*手机KEY*/
	var $postMobiKey;
	/*sessionID*/
	var $postSessionId;
	/*菜品名称*/
	var $pic_name;
	/*主图路径*/
	var $pic_path;
	/*菜品介绍*/
	var $pic_caption;
	/*餐馆名*/
	var $restaurant;
	/*所在城市*/
	var $city;
	/*X坐标*/
	var $pic_x;
	/*Y坐标*/
	var $pic_y;
	/*发表时间*/
	var $pic_time;
	/*餐馆地址*/
	var $pic_addr;
	/*执行动作*/
	var $postAction;
	/*用户数据*/
	var $userArr;
	/*用户ID*/
	var $userid;
	/*条件*/
	var $criteria;
	/*条件对应数组*/
	var $criArr = array("city", "user", "time", "latest", "tasty", "beautiful", "want");
	/* 数据库操作表*/
	var $tab;
	/*每次返回客户端的条数*/
	var $rNum = "10";
	var $cursor = 0;
	var $offset = 10;
	var $nicename;

	public function index() {
		echo"1";
		/*如果直接访问此页面acction*/
		$post = $this -> input -> post('action');
		if (!$post || empty($post)) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_no'), TRUE));
			exit ;
		} else {
			$this -> postAction = $this -> input -> post('action');
		}
		echo "2";
		/*判断用户权限*/
		$this -> postMobiKey = $this -> input -> post('mobikey');
		$this -> postSessionId = $this -> input -> post('sessionid');
		$this -> userArr = $this -> sx_useropen_mod -> user_con($this -> postMobiKey, $this -> postSessionId, TRUE);
		echo "3";
		if (!$this -> userArr) {
			/*session或者Key过期，验证失败*/
			print($this -> fengchaoclass -> showhckmsg("-1", NULL, $this -> lang -> line('hck_no'), TRUE));
			exit ;
		}
		echo "4";
		$this -> userid = $this -> userArr['userid'];
		$this -> nicename = $this-> userArr['nicename'];
		/*在如帖子操作mod*/
		$this -> load -> model("sx_article_mod");
		/*根据不同的action执行操作*/
		switch ($this -> postAction) {
			/* 执行add操作*/
			case 'addArticle' :
				$this -> addarticle();
				break;
			/* 执行条件查询显示*/
			case 'getArticleList' :
				$this -> getarticlelist();
				break;
			/*添加评论*/
			case 'addComment' :
				$this -> addcomment();
				break;
			/* 添加收藏*/
			case 'setFavorites' :
				$this -> setfavorites();
				break;
			/* 显示个人收藏*/
			case 'getFavorites' :
				$this -> getfavorites();
				break;
			/*删除个人收藏*/
			case 'delFavorites' :
				$this -> delfavorites();
				break;
			default :
				print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_no'), TRUE));
				break;
		}

	}

	/**
	 * 添加帖子
	 */
	private function addarticle() {

		/*获取菜品名称*/
		$this -> pic_name = $this -> input -> post("pic_name");
		/*获取菜品图片的路径*/
		$this -> pic_path = $this -> input -> post("pic_path");
		/*获取城市*/
		$this -> city = $this -> input -> post('city');
		/*获取发贴人*/
		$this -> userid = $this -> userArr['userid'];
		/*获取餐馆名称*/
		$this -> restaurant = $this -> input -> post("restaurant");
		/*餐馆地址*/
		$this -> pic_addr = $this -> input -> post("pic_addr");
		/*菜品介绍*/
		$this -> pic_caption = $this -> input -> post("pic_caption");
		/*菜品坐标*/
		$this -> pic_x = $this -> input -> post("pic_x");
		$this -> pic_y = $this -> input -> post("pic_y");
		/* 定义数据库操作表*/
		$this -> tab = "sx_article";
		/*判断是否有空值*/
		if (empty($this -> userid) || empty($this -> pic_name) || empty($this -> pic_addr) || empty($this -> pic_path) || empty($this -> city) || empty($this -> restaurant)) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_null'), TRUE));
			exit ;
		}
		$dbArr = array("user_id" => $this -> userid, "pic_path" => $this -> pic_path, "pic_caption" => $this -> pic_caption,"user_nicename"=>$this -> nicename, "pic_addr" => $this -> pic_addr, "pic_x" => $this -> pic_x, "pic_y" => $this -> pic_y, "pic_name" => $this -> pic_name, "restaurant" => $this -> restaurant, "city" => $this -> city);
		/*判断操作是否成功*/
		if ($this -> sx_article_mod -> addarticle($dbArr, $this -> tab) == FALSE) {

			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

	/*首页列表显示*/
	private function getarticlelist() {
		
		$this -> city = $this->input->post("city");
		empty($this->input->post("cursor"))?$this->cursor:$this->cursor = $this->input->post("cursor");
		empty($this->input->post("offset"))?$this->offset:$this->offset = $this->input->post("offset");
		$pageArr = array($this->offset,$this->cursor);
		//$this -> sx_article_mod ->getarticlelist($all,$n = false);
		
	}

	/**
	 * 添加个人收藏
	 */
	private function setfavorites() {
		/*操作收藏表*/
		$this -> tab = "sx_favorites";
		/*获取菜品id*/
		$picid = $this -> input -> post("pic_id");
		/*判断数据是否为空*/
		if (empty($picid) || empty($this -> userid)) {
			print($this -> fengchaoclass -> showhckmsg("", NULL, $this -> lang -> line('hck_parame'), TRUE));
			exit ;
		}
		/*插入数据库数据*/
		$dbArr = array("user_id" => $this -> userid, "pic_id" => $picid);

		if ($this -> sx_article_mod -> addarticle($dbArr, $this -> tab) == FALSE) {
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

	/**
	 * 获取个人收藏
	 */
	private function getfavorites() {
		$this -> criteria = "";
	}

	/**
	 * 删除个人收藏
	 */
	private function delfavorites() {
		
	}

	/**
	 * 添加评论
	 */
	private function addcomment() {
		/*设定操作的表名*/
		$this -> tab = "sx_comment";
		/*获取要评论的帖子ID*/
		$pic_id = $this -> input -> post("pic_id");
		/*获取评论内容*/
		$content = $this -> input -> post("content");
		/*要添加的评论内容*/
		$dbArr = array("author" => $this -> userid, "pic_id" => $pic_id, "content" => $content);
		if ($this -> sx_article_mod -> addarticle($dbArr, $this -> tab) == FALSE) {
			
			print($this -> fengchaoclass -> showhckmsg("1", NULL, $this -> lang -> line('hck_article_add_N'), TRUE));
		} else {
			print($this -> fengchaoclass -> showhckmsg("0", NULL, $this -> lang -> line('hck_article_add_Y'), TRUE));
		}

	}

}
?>